package com.solrj.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.solr.common.SolrDocumentList;
import org.slf4j.LoggerFactory;

public class DefaultSolrOperator implements SolrjOperator { 
    
    private org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass()); 
    private SolrjQuery solrjQuery; 
 
    public void setSolrjQuery(SolrjQuery solrjQuery) { 
        this.solrjQuery = solrjQuery; 
    } 
 
    public List<Object> querySolrResult(Object propertyDO, 
            Object compositorDO, Long startIndex, Long pageSize) 
            throws Exception { 
        Map<String, String> propertyMap = new TreeMap<String, String>(); 
        //排序有顺序,使用TreeMap 
        Map<String, String> compositorMap = new TreeMap<String, String>(); 
        try { 
            propertyMap = SolrjCommonUtil.getSearchProperty(propertyDO); 
            compositorMap = SolrjCommonUtil.getSearchProperty(compositorDO); 
        } catch (Exception e) { 
            logger.error("SolrjCommonUtil.getSearchProperty() is error !"+ e); 
        } 
        SolrDocumentList solrDocumentList = solrjQuery.query(propertyMap, compositorMap, 
                startIndex, pageSize); 
        List<Object> resultList = new ArrayList<Object>(); 
        for (int i = 0; i < solrDocumentList.size(); i++) { 
            resultList.add(solrDocumentList.get(i)); 
        } 
        return resultList; 
    } 
 
    public Long querySolrResultCount(SolrPropertyDO propertyDO, 
            Object compositorDO) throws Exception { 
        Map<String, String> propertyMap = new TreeMap<String, String>(); 
        Map<String, String> compositorMap = new TreeMap<String, String>(); 
        try { 
            propertyMap = SolrjCommonUtil.getSearchProperty(propertyDO); 
            compositorMap = SolrjCommonUtil.getSearchProperty(compositorDO); 
        } catch (Exception e) { 
            logger.error("SolrjCommonUtil.getSearchProperty() is error !" + e); 
        } 
        SolrDocumentList solrDocument = solrjQuery.query(propertyMap, compositorMap, 
                null, null); 
        return solrDocument.getNumFound(); 
    } 
 
} 